package com.lifeng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lifeng.constant.ResCodeConstant;
import com.lifeng.domain.SysUser;
import com.lifeng.service.SysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author:lifeng
 * @date:2020/4/20 18:21
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private SysUserService sysUserService;

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder binder){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        binder.registerCustomEditor(Date.class,dateEditor);
    }

    @RequestMapping("/list")
    public Map<String,Object> list(Integer pageNum,Integer pageSize,SysUser sysUser){
        Map<String,Object> res = new HashMap<>();
        IPage<SysUser> pages = sysUserService.listPage(pageNum,pageSize,sysUser);
        res.put("code", ResCodeConstant.C0);
        res.put("rows", pages.getRecords());
        res.put("total",pages.getTotal());
        return res;
    }

    @RequestMapping("/add")
    public Object add(SysUser sysUser){
        Map<String,Object> res = new HashMap<>();
        sysUserService.add(sysUser);
        res.put("code", ResCodeConstant.C0);
        return res;
    }

    @RequestMapping("/update")
    public Object update(SysUser sysUser){
        Map<String,Object> res = new HashMap<>();
        sysUserService.update(sysUser);
        res.put("code", ResCodeConstant.C0);
        return res;
    }

    @RequestMapping("/delete")
    public Object delete(String ids){
        Map<String,Object> res = new HashMap<>();
        String[] idsS = ids.split(",");
        List<Integer> idsI = new ArrayList<>();
        for(String id:idsS){
            idsI.add(Integer.parseInt(id));
        }
        sysUserService.delete(idsI);
        res.put("code", ResCodeConstant.C0);
        return res;
    }
}
